//该文件用于创建整个应用的路由器
import VueRouter from "vue-router";
import About from '../pages/About'
import Home from '../pages/Home'
import News from '../pages/News'
import Message from '../pages/Message'
import Detail from '../pages/Detail'
const router = new VueRouter({
    //注意这里配置项数组是 routes
    routes: [
        {
            meta: { title: '关于' },
            name: 'about',
            path: '/about',
            component: About
        },
        {
            meta: { title: '主页' },
            name: 'home',
            path: '/home',
            component: Home,
            children: [
                {
                    meta: { title: '新闻', isAuth: true, },
                    name: 'news',
                    path: 'news',
                    component: News,
                },
                {
                    meta: { title: '消息', isAuth: true, },
                    name: 'message',
                    path: 'message',
                    component: Message,
                    children: [
                        {
                            meta: { title: '详情', isAuth: true, },
                            name: 'detail',
                            path: 'detail',
                            component: Detail,
                            //第一种写法：props值为对象，该对象中所有的key-value的组合最终都会通过props传给Detail组件
                            // props:{a:900}

                            //第二种写法：props值为布尔值，布尔值为true，则把路由收到的所有params参数通过props传给Detail组件
                            // props:true

                            //第三种写法：props值为函数，该函数返回的对象中每一组key-value都会通过props传给Detail组件
                            props({ query }) {
                                return { id: query.id, title: query.title }
                            }
                        }
                    ]
                }
            ]
        }
    ]
})

router.beforeEach((to, from, next) => {
    console.log('前置路由守卫', to.name, from.name);
    if (to.meta.isAuth) {  //判断是否需要鉴权
        if (localStorage.school === '黑马') {
            next()
        } else {
            alert('学校名字不对')
        }
    } else {
        next()
    }
})

router.afterEach((to, from) => {
    console.log('后置路由守卫', to.name, from.name);
    document.title = to.meta.title || '起飞系统'
})

export default router